package com.team2.cyber_eye;

import java.util.ArrayList;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import com.team2.cyber_eye.ActivityHelpers.AdapterStore;
import com.team2.cyber_eye.ActivityHelpers.ImageLoader;
import com.team2.cyber_eye.ActivityHelpers.Util;
import com.team2.cyber_eye.background_task.RetrieveStoresTask;
import com.team2.cyber_eye.R;
import com.team2.shared.Product;
import com.team2.shared.Store;

public class ProductInfo extends CyberActivity implements RetrieveStoresTask.LoadingTaskFinishedListener, 
															LocationListener {

	AdapterStore adapter;
	public ImageLoader imageLoader; 
	ListView list;
	ProgressDialog progDialog;
	ArrayList<Store> stores;
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState, R.layout.activity_product_info, "PRODUCT DETAILS");
		
		Util.StartLocationGathering(this, this);
		
		showDialog(0);
		
		imageLoader = new ImageLoader(this.getApplicationContext());

		list = (ListView)this.findViewById(R.id.storeList);
		ScrollView TopLayout = (ScrollView)this.findViewById(R.id.ProductInfoZone);
		
		ImageView productImage = (ImageView)this.findViewById(R.id.ProductImage);
		TextView productName = (TextView)this.findViewById(R.id.ProductName);
		TextView productDesc = (TextView)this.findViewById(R.id.ProductDescription);
		TextView productPrice = (TextView)this.findViewById(R.id.ProductPrice);
		TextView productLocalAvailability = (TextView)this.findViewById(R.id.localAvailabilityText);
		TextView productOnlineAvailability = (TextView)this.findViewById(R.id.onlineAvailabilityText);
		ImageView localAvailabilityImage = (ImageView)this.findViewById(R.id.localAvailabilityImage);
		ImageView onlineAvailabilityImage = (ImageView)this.findViewById(R.id.onlineAvailabilityImage);
		

		//ensures that split is roughly 50% top 50% bottom
		LayoutParams lpt = TopLayout.getLayoutParams();
		lpt.height = (int)(getWindowManager().getDefaultDisplay().getHeight() / 2f - 80);
		TopLayout.setLayoutParams(lpt);

		Product product = (Product)this.getIntent().getParcelableExtra("Product");
		
		if (product != null)
		{
			imageLoader.DisplayImage(product.getImageLink(), productImage);
			
			productName.setText(product.getTitle());
			
			String description = product.getDescription();
			
			if(description.equals("null") || description.length() == 0)
			{
				productDesc.setText("");
				productDesc.setVisibility(View.GONE);
			}
			else
			{
				productDesc.setText(description);
			}
			
			String price = product.getPrice();
			
			if(price.length() > 0){
				productPrice.setText(price);
			}
			
			String localAvailability = product.getInStoreAvailability();
			
			if(localAvailability.length() > 0){
				productLocalAvailability.setText(localAvailability.toUpperCase());
			}
			
			String onlineAvailability = product.getOnlineAvailablility();
			
			if(onlineAvailability.length() > 0){
				productOnlineAvailability.setText(onlineAvailability.toUpperCase());
			}
			
			if(!localAvailability.toLowerCase().contains("not") && localAvailability != null && !localAvailability.equals("") ){
				localAvailabilityImage.setBackgroundResource(R.drawable.available);
			}else{
				localAvailabilityImage.setBackgroundResource(R.drawable.not_available);
			}
			
			if(!onlineAvailability.toLowerCase().contains("not") && onlineAvailability != null && !onlineAvailability.equals("")){
				onlineAvailabilityImage.setBackgroundResource(R.drawable.available);
			}else{
				onlineAvailabilityImage.setBackgroundResource(R.drawable.not_available);
			}
			
			//sort by price
			double longitude = Util.GetLongitugeLatitude(this)[0];
			double latitude = Util.GetLongitugeLatitude(this)[1];
			int radius = (int) Options.SearchDistance;
			
			Object param1 = (Object) product;
			Object param2 = (Object) latitude;
			Object param3 = (Object) longitude;
			Object param4 = (Object) radius;
			
			RetrieveStoresTask storesTask = new RetrieveStoresTask(this, this);
			storesTask.execute(param1, param2, param3, param4);
					
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    
	    switch (requestCode) {	    	
		    case 5:
		    	Util.StartLocationGathering(this, this);
		    	break;
	    }
	}

	protected void ItemClick(int position)
	{
		try
		{
			//display Directions
			Intent intent = new Intent();
			intent.setClass(ProductInfo.this, ShowMapView.class);

			intent.putExtra("originLat", Util.getLatitude());
			intent.putExtra("originLng", Util.getLogitude());
			intent.putExtra("destLat", stores.get(position).getLatitude());
			intent.putExtra("destLng", stores.get(position).getLongitude());

			startActivity(intent);		
		}
		catch (Exception ex)
		{
			Util.DisplayError(this, "Error displaying store directions", ex);
		}
	}

	@Override
	public void onTaskFinished(ArrayList<Store> stores) {
		//Create store list
		this.stores = stores;

		adapter = new AdapterStore(this, stores);
		list.setAdapter(adapter);
		list.setOnItemClickListener(new OnItemClickListener() { 
			@Override public void onItemClick(AdapterView<?> a, View v, int position, long id) 
			{ 
				ItemClick(position); 
			} 
		});
		
		if(Util.isGPSEnabled(this)){
			Util.StartLocationGathering(this, this);
		}

		progDialog.dismiss();
	}
	
	@Override
	protected Dialog onCreateDialog(int id){
		 progDialog = new ProgressDialog(this, R.style.Theme_MyDialog);
		 progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
	     progDialog.setTitle("In Progress");
	     progDialog.setMessage("Retrieving product info and store list...");
	     return progDialog;
	}

	@Override
	public void onLocationChanged(Location location) {
		
		Util.setLatitude(location.getLatitude());
		Util.setLogitude(location.getLongitude());
		
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}
